﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;
namespace Web
{
    public partial class AdminMaster : System.Web.UI.Page
    {
        public static Module_Page Pages = new Module_Page();
        private string _IdCard = "";
        private string _Name = "";
        private string _Tel = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterUser"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }

            if (Request.QueryString["page"] != null && Request.QueryString["page"] != string.Empty)
            {
                if (PageValidate.IsNumber(Request.QueryString["page"]))
                {
                    Pages.PageNum = int.Parse(Request.QueryString["page"]);
                }
            }
            if (PageValidate.QueryStringIsEmpty("Name"))
            {
                _Name = "";
            }
            else
            {
                _Name = PageValidate.QueryString("Name");
            }
            if (PageValidate.QueryStringIsEmpty("IdCard"))
            {
                _IdCard = "";
            }
            else
            {
                _IdCard = PageValidate.QueryString("IdCard");
            }
            if (PageValidate.QueryStringIsEmpty("Tel"))
            {
                _Tel = "";
            }
            else
            {
                _Tel = PageValidate.QueryString("Tel");
            }
            if (!IsPostBack)
            {
                if (Session["begin"]!=null)
                {
                    //检查批量注册中的错误信息
                    if (Session["nameofIdCard"] != null)
                    {
                        string nameofIdCard = Session["nameofIdCard"].ToString();
                        if (Session["nameofEmail"] != null)
                        {
                            string nameofEmail = Session["nameofEmail"].ToString();
                            Utility.ShowBoxyAlert("注意:请下列人检查身份证号和邮箱,然后重新将他们的信息上传,其他人信息注册成功：" + nameofIdCard + nameofEmail, this);
                            Session.Remove("nameofEmail");
                        }
                        else
                        {
                            Utility.ShowBoxyAlert("注意:请下列人检查身份证号,然后重新将他们的信息上传,其他人信息注册成功：" + nameofIdCard, this);
                            Session.Remove("nameofIdCard");
                        }
                    }
                    else
                    {
                        if (Session["nameofEmail"] != null)
                        {
                            string nameofEmail = Session["nameofEmail"].ToString();
                            Utility.ShowBoxyAlert("注意:请下列人检查邮箱,然后重新将他们的信息上传,其他人信息注册成功：" + nameofEmail, this);
                            Session.Remove("nameofEmail");
                        }
                        else
                        {
                            Utility.ShowBoxyAlert("批量添加用户成功", this);
                            Session["begin"] = null;
                        }
                    }                   
                }
                
                LoadList();
            }
        }

        protected void LoadList()
        {
            Pages.AllCount = UserLinkExtend.GetCount(_Name, _IdCard, _Tel);
            Pages.PageSize = 20;
            Pages.PageName = GetPageName();
            Pages.Compute();
            this.PageLabel.Text = Pages.GetPage();
            IList<UserLinkExtendInfo> list = UserLinkExtend.GetList(_Name, _IdCard, _Tel, Pages.First, Pages.Max);
            this.UserLists.DataSource = list;
            this.UserLists.DataBind();
        }
        private string GetPageName()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("AdminMaster.aspx?id=1");
            if (!PageValidate.IsEmpty(this._Name))
            {
                builder.Append("&Name=");
                builder.Append(this._Name);
            }
            if (!PageValidate.IsEmpty(this._IdCard))
            {
                builder.Append("&IdCard=");
                builder.Append(this._IdCard);
            }
            if (!PageValidate.IsEmpty(this._Tel))
            {
                builder.Append("&Tel=");
                builder.Append(this._Tel);
            }
            builder.Append("&Page=");
            return builder.ToString();
        }
        public string FormatRole(object str)
        {
            return Role.GetRoleById(Convert.ToInt32(str));
        }
        public string FormatStatus(object str)
        {
            if (str.ToString() == "1")
            {
                return "未审核";
            }
            if (str.ToString() == "2")
            {
                return "已审核";
            }
            if (str.ToString() == "3")
            {
                return "未激活";
            }
            return "未定义";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (PageValidate.FormIsEmpty("Name"))
            {
                this._Name = "";
            }
            else
            {
                this._Name = PageValidate.Form("Name");
            }
            if (PageValidate.FormIsEmpty("IdCard"))
            {
                this._IdCard = "";
            }
            else
            {
                this._IdCard = PageValidate.Form("IdCard");
            }
            if (PageValidate.FormIsEmpty("Tel"))
            {
                this._Tel = "";
            }
            else
            {
                this._Tel = PageValidate.Form("Tel");
            }
            this.LoadList();
        }
    }
}
